<template>
	<view class="container">
		<!-- 内容主体 -->
		<view class="content">
			<view class="title">
				<text>重置密码</text>
			</view>
			<form @submit="onSubmit" class="form">
				<view class="form-item">
					<input type="text" v-model="email" placeholder="请输入邮箱" style="flex: 1; margin-right: 10px;" />
					<button :disabled="isCounting" @click="sendCode"
						:class="{ 'btn': true, 'btn-disabled': isCounting }">
						{{ isCounting ? `验证码已发送（${time}）` : '发送验证码' }}
					</button>
				</view>
				<view class="form-item">
					<input type="text" v-model="code" placeholder="请输入验证码" />
				</view>
				<view class="form-item">
					<input type="password" v-model="password" placeholder="请输入新密码" />
				</view>
				<button form-type="submit" class="submit-btn">重置</button>
			</form>
			<view class="back-login">
				<navigator url="/pages/login/login">返回登录</navigator>
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<text>© - 联系校事达官方客服：6666666</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				code: '',
				password: '',
				time: 0,
				isCounting: false
			};
		},
		methods: {
			sendCode() {
				if (!this.validateEmail()) return;
				this.time = 60;
				this.isCounting = true;
				const timer = setInterval(() => {
					if (this.time > 1) {
						this.time--;
					} else {
						this.isCounting = false;
						clearInterval(timer);
					}
				}, 1000);
				// 这里可以添加发送验证码的接口调用
			},
			validateEmail() {
				const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
				if (!emailRegex.test(this.email)) {
					uni.showToast({
						title: '请输入有效的邮箱地址',
						icon: 'none'
					});
					return false;
				}
				return true;
			},
			validatePassword() {
				const passwordRegex = /^(.+){3,12}$/;
				if (!passwordRegex.test(this.password)) {
					uni.showToast({
						title: '密码必须3到12位',
						icon: 'none'
					});
					return false;
				}
				return true;
			},
			onSubmit() {
				if (!this.validateEmail() || !this.validatePassword()) return;
				if (!this.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}
				// 这里可以添加重置密码的接口调用
				uni.showToast({
					title: '重置成功',
					icon: 'success'
				});
				uni.navigateTo({
					url: "/pages/login/login"
				})
			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		min-height: 93vh;
	}

	.logo image {
		width: 150px;
		height: auto;
	}

	.nav {
		display: flex;
	}

	.nav navigator {
		margin-left: 20px;
		color: #333;
		text-decoration: none;
	}

	.content {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 20px;
	}

	.title {
		margin-bottom: 20px;
		color: #333;
	}

	.form {
		width: 100%;
		max-width: 400px;
	}

	.form-item {
		display: flex;
		flex-direction: row;
		/* 修改为水平布局 */
		margin-bottom: 20px;
		align-items: center;
		/* 垂直居中对齐 */
	}

	.form-item input {
		padding: 12px;
		border: 1px solid #ccc;
		border-radius: 4px;
		font-size: 16px;
	}

	.btn {
		padding: 4px;
		border: none;
		border-radius: 4px;
		background-color: #1E9FFF;
		color: white;
		font-size: 16px;
		cursor: pointer;
		white-space: nowrap;
		/* 防止按钮文字换行 */
		height: 50px;
	}

	.btn-disabled {
		background-color: #1E9FFF;
		cursor: not-allowed;
	}

	.submit-btn {
		width: 100%;
		padding: 5px;
		border: none;
		border-radius: 4px;
		background-color: #1E9FFF;
		color: #fff;
		font-size: 16px;
		cursor: pointer;
		height: 50px;
	}

	.back-login {
		margin-top: 20px;
	}

	.back-login navigator {
		color: #1E9FFF;
		text-decoration: none;
	}

	.footer {
		text-align: center;
		padding: 20px;
		background-color: #f8f8f8;
	}
</style>